<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>系统用户管理</title>
    <link rel="stylesheet" href="../../../layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../layui/layuiAdmin/css/admin.css" media="all">
    <style>
        .site-border-default fieldset {
            border-top: 1px solid #eee;
        }
        .layui-inline{


            /* 元素距离上，左都为50% */
            left: 20%;

            /* 让元素的左外边距，上外边距为元素宽高的1/2 注意margin是负距*/
        }
    </style>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" style="border-radius: 0;border-top: 4px solid #d2d2d2;">
            <div class="layui-form-item">
                <div class="layui-inline">
                        <div class="layui-form-item layui-hide">
                            <div class="layui-input-inline st">
                                <input type="text" id="hospitalId" name="hospitalId" lay-verify="required" class="layui-input layui-hide">
                            </div>
                        </div>
                    <div style="display: flex;">
                        <div>
                            <label class="layui-form-label">医院名称:</label>
                            <div class="layui-input-inline">
                                <input type="text" name="hospitalName" id="hospitalName" placeholder="请输入用户名" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div>
                            <label class="layui-form-label">医院等级:</label>
                            <div class="layui-input-inline">
                                <input type="text" name="hospitalLevel" id="hospitalLevel" placeholder="请输入用户名" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div style="display: flex;">
                        <div>
                            <label class="layui-form-label">医院别称:</label>
                            <div class="layui-input-inline">
                                <input type="text" name="hospitalNickname" id="hospitalNickname" placeholder="请输入用户名" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div>
                            <label class="layui-form-label">医院性质:</label>
                            <div class="layui-input-inline">
                                <input type="text" name="hospitalNature" id="hospitalNature" placeholder="请输入用户名" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div style="display: flex;">
                        <div>
                            <label class="layui-form-label">联系电话:</label>
                            <div class="layui-input-inline">
                                <input type="text" name="hospitalTel" id="hospitalTel" placeholder="请输入用户名" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div>
                            <label class="layui-form-label">服务热线:</label>
                            <div class="layui-input-inline">
                                <input type="text" name="serviceHotline" id="serviceHotline" placeholder="请输入用户名" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div style="display: flex;">
                        <div>
                            <label class="layui-form-label">医院联系人:</label>
                            <div class="layui-input-inline">
                                <input type="text" name="hospitalContacts" id="hospitalContacts" placeholder="请输入用户名" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div>
                            <label class="layui-form-label">联系人电话:</label>
                            <div class="layui-input-inline">
                                <input type="text" name="contactsPhone" id="contactsPhone" placeholder="请输入用户名" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <fieldset class="site-border-default"></fieldset>

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-useradmin" data-type="add">新增部门科室</button>
            </div>
            <!--  表格数据加载 -->
            <table id="LAY-user-manage" lay-filter="LAY-user-manage"></table>
            <script type="text/html" id="table-useradmin-webuser">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
            </script>
            <!--状态判断-->
            <script type="text/html" id="statusTpl">

                {{#  if(d.status == 1){ }}
                <button class="layui-btn layui-btn-xs">正常</button>
                {{#  } else if(d.status == 2) { }}
                <button class="layui-btn layui-btn-danger layui-btn-xs">冻结</button>
                {{#  } }}
            </script>


            <!--  创建时间格式化处理 -->
            <script type="text/html" id="createTimeTpl">
                {{formatDate(d.createTime)}}
            </script>

        </div>
    </div>
</div>

<script src="../../../layui/layui.js"></script>
<script type="text/javascript">
    // 日期格式转换函数
    function formatDate(str) {
        if(str != null){
            var now = new Date(str);
            var year = now.getFullYear();  //取得4位数的年份
            var month = now.getMonth() + 1;  //取得日期中的月份，其中0表示1月，11表示12月
            var date = now.getDate();      //返回日期月份中的天数（1到31）
            var hours = now.getHours();
            var minutes = now.getMinutes();
            var seconds = now.getSeconds();
            return year + "-" + month + "-" + date +" "+hours+":"+minutes+":"+seconds;
        }
        return "";
    }
</script>
<script type="text/javascript">


    layui.config({
        base: '../../../layui/layuiAdmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['layer','table','form'], function(){
        var $ = layui.$
            ,form = layui.form
            ,layer = layui.layer
            ,table = layui.table;
        //加载table\

        var storedUsername = localStorage.getItem('hospitalId');
        console.log(storedUsername)
        table.render({
            elem: '#LAY-user-manage'
            ,url:'http://localhost:8888/dept/findSysDoctorAll' //后端接口
            ,cellMinWidth: 80 //全局定义常规单元格的最小宽度
            ,title: '部门信息'
            , where: {
                hospitalId:storedUsername,
            }
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                //获得医生数据
                ,{field:'deptId', title: '部门ID', width:80}
                ,{field:'deptName', title: '部门名称', width:130}
                ,{field:'number', title: '人数', width:130}
                ,{field:'upDepet', title: '上级部门id', width:130,hide:true}
                ,{field:'upDeptName', title: '上级部门', width:130}
                ,{field:'hospitalId', title: '所属医院Id', width:130,hide:true}
                ,{field:'hospitalName', title: '所属医院', width:130}
                ,{field:'remark', title: '备注', width:130}
                ,{field:'createDate', title: '创建时间', width:130}
                ,{field:'status', title: '状态', width:130,templet: '#statusTpl'}
                ,{fixed: 'right', title:'操作', toolbar: '#table-useradmin-webuser',width:230}

            ]]
            ,page: {
                curr: 1 //设定初始在第 5 页
                ,limit: 5 //设定初始每页显示 5 条
            },
            limits:[5,10,15,20]
        });
        //监听行工具事件
        table.on('tool(LAY-user-manage)', function(obj){
            var data = obj.data;
            if(obj.event === 'edit'){
                layer.open({
                    type: 2
                    ,title: '修改医院'
                    ,skin: 'layui-layer-molv'
                    ,content: 'editDept.html'
                    ,maxmin: true
                    ,area: ['350px', '400px']
                    ,btn: ['确定', '取消']
                    // 加载弹出层之前获取选中数据，进行回显
                    ,success: function (layero,index){

                        var body = layer.getChildFrame("body",index);
                        var contentWindow = layero.find('iframe')[0].contentWindow;//得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        body.find("input[name=deptId]").val(data.deptId);

                        body.find("input[name=upDepet]").val(data.upDepet);
                        body.find("input[name=deptName]").val(data.deptName);
                        body.find("input[name=hospitalId]").val(data.hospitalId);
                        body.find("input[name=remark]").val(data.remark);
                        // 性别
                        body.find("input[name=createTime]").val(data.createTime);

                    }
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submitID = 'LAY-user-front-submit'
                            ,submit = layero.find('iframe').contents().find('#'+ submitID);
                        //监听提交
                        iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                            var field = data.field; //获取提交的字段
                            $.ajax({
                                url:"http://localhost:8888/dept/updates", //请求后端接口地址
                                type:"post", //请求方式get | post
                                data:JSON.stringify(field), //提交json字符串对象参数
                                contentType:"application/json;charset=utf-8", //发送信息至服务器时内容编码类型
                                dataType:"json", //后端返回参数类型
                                success:function (res){ //请求成功后回调函数res = JSONObject.toJSONString(resultMap)
                                    if(res.code == 0){//成功
                                        layer.msg(res.message);//弹出提示
                                        table.reload('LAY-user-manage'); //数据刷新
                                        layer.close(index); //关闭弹层
                                    }
                                    if(res.code == -1){//系统出问题
                                        layer.msg(res.message,{icon: 5});//弹出提示
                                    }
                                }
                            })
                        });
                        submit.trigger('click');
                    }
                });

            }
        });
        var active = {
            add: function(){
                layer.open({
                    type: 2
                    ,title: '添加部门信息'
                    ,skin: 'layui-layer-molv'
                    ,content: 'addDept.html'
                    ,maxmin: true
                    ,area: ['350px', '400px']
                    ,btn: ['确定', '取消']
                    ,yes: function (index,layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submitID = 'LAY-user-front-submit'
                            ,submit = layero.find('iframe').contents().find('#'+ submitID);
                        //监听提交--隐藏按钮
                        iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                            var field = data.field; //获取提交的字段
                            // 通过Ajax方式将参数提交到后端--保存数据库中
                            $.ajax({
                                url:"http://localhost:8888/dept/save",
                                type:"post",
                                data:JSON.stringify(field),
                                contentType: "application/json;charset=utf-8", //提交参数类型为json格式
                                dataType:"json",
                                success:function (res){
                                    if(res.code == 0){//成功
                                        layer.msg(res.message);//弹出提示
                                        table.reload('LAY-user-manage'); //数据刷新
                                        layer.close(index); //关闭弹层
                                    }else{//系统出问题
                                        layer.msg(res.message,{icon: 5});//弹出提示
                                    }
                                }
                            })
                        });
                        submit.trigger('click');
                    }

                });
            }
        };





        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });
</script>
</body>
</html>